//
//  JKDIYHeader.m
//  JKRefreshHeader
//
//  Created by 王冲 on 2019/6/17.
//  Copyright © 2019 JK科技有限公司. All rights reserved.
//

#import "JKDIYHeader.h"

@interface JKDIYHeader ()

/** 开关 */
@property (nonatomic, weak) UISwitch *s;
/** logo */
@property (nonatomic, weak) UIImageView *logo;

@end

@implementation JKDIYHeader

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        UISwitch *s = [[UISwitch alloc] init];
        [self addSubview:s];
        self.s = s;
        
        UIImageView *logo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
        [self addSubview:logo];
        self.logo = logo;
        
        // 刷新区间内容的高度
        // self.xmg_height = 70;
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.logo.jk_centerX = self.jk_width * 0.5;
    self.logo.jk_y =  -  3 * self.logo.jk_height;
    
    self.s.jk_centerX = self.jk_width * 0.5;
    self.s.jk_centerY = self.jk_height * 0.5;
}

#pragma mark - 重写Header内部的方法
- (void)setState:(MJRefreshState)state
{
    [super setState:state];
    
    if (state == MJRefreshStateIdle) {
        // 下拉可以刷新
        
        [self.s setOn:NO animated:YES];
        
        [UIView animateWithDuration:0.25 animations:^{
            self.s.transform = CGAffineTransformIdentity;
        }];
    } else if (state == MJRefreshStatePulling) {
        // 松开立即刷新
        
        [self.s setOn:YES animated:YES];
        
        [UIView animateWithDuration:0.25 animations:^{
            self.s.transform = CGAffineTransformMakeRotation(M_PI_2);
        }];
    } else if (state == MJRefreshStateRefreshing) {
        // 正在刷新
        
        [self.s setOn:YES animated:YES];
        
        [UIView animateWithDuration:0.25 animations:^{
            self.s.transform = CGAffineTransformMakeRotation(M_PI_2);
        }];
    }
}

@end
